﻿// Jose F Valdés
using System;
using System.IO;

namespace DamCommander
{
    class FileList
    {
        FileInfo[] f;
        DirectoryInfo[] d;
        string path;

        public FileList()
        {
            DirectoryInfo dir = new DirectoryInfo(@"C:\");
            f = dir.GetFiles();
            d = dir.GetDirectories();
        }

        public FileList(string path)
        {
            DirectoryInfo dir = new DirectoryInfo(path);
            ChangePath(path);
            f = dir.GetFiles();
            d = dir.GetDirectories();
        }

        public void ChangePath(string path)
        {
            this.path = path;
            Refresh();
        }

        public void Refresh()
        {
            // Re-read the contents of the file list
            DirectoryInfo dir = new DirectoryInfo(path);
            f = dir.GetFiles();
            d = dir.GetDirectories();
        }

        public int GetSize()
        {
            // Get the length of the FileList
            return f.Length;
        }

        public long GetSizeDirectories()
        {

            // Get the length of the Directory list
            return d.Length;
        }

        public string GetName(int i)
        {
            // Get name of item number i
            return f[i].Name;
        }

        public string GetNameDirectory(int i)
        {
            return d[i].Name;
        }

        public string GetDate(int i)
        {
            // Get date of item number i, in format DD/MM/YYYY
            DateTime t = f[i].CreationTime;
            string time = "" + t.Day + "/" + t.Month + "/" + t.Year;
            return time;
        }

        public int GetTotalSize()
        {
            return d.Length + f.Length;
        }

        public long GetSize(int i)
        {
            // Get size of item number i
            return f[i].Length;
        }

        public void ShowList()
        {
            for (int i = 0; i < GetSizeDirectories(); i++)
                Console.WriteLine("   "+GetNameDirectory(i));
            for (int i = 0; i < GetSize(); i++)
                Console.WriteLine("   " + GetName(i));
        }

        public void ShowList(int position)
        {
            if (position <= 18)
                ShowList(0, 18);
            else
            {
                int counter = 1;
                while (position > 18)
                {
                    position -= 18;
                    counter++;
                }
                ShowList(18 , 18*counter);
            }
        }

        // show list from postition to other
        public void ShowList(int init, int end)
        {
            // Doing this, we will never exceed the array
            if (end > GetTotalSize())
                end = GetTotalSize();
            if (!(init > GetTotalSize()))
            {
                int j = 0;

                for (int i = init; i < end; i++)
                    if (i < d.Length)
                        Console.WriteLine("  " + GetNameDirectory(i));
                    else
                    {
                        Console.WriteLine("  " + GetName(j));
                        j++;
                    }
            }
            else
                Console.WriteLine("Error showing list");
        }
    }
}
